package org.pr.logoupdate.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.filters.Watermark;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.geometry.Positions;

public class ImageUtil {
	private static Logger logger = Logger.getLogger(ImageUtil.class);

	public ImageUtil() {
		// TODO Auto-generated constructor stub
	}
	
	public static void cutImage(String path, int width, int height,
			int cutLeft, int cutTop, int cutWidth, int cutHeight) throws IOException
	{
		logger.debug("start to cutImage for " + path);
		File file = new File(path);
		int[] data = getWidthHeight(path);
		int originalW = data[0];
		int originalH = data[1];
		int x = cutLeft * originalW / width;
		int y = cutTop * originalH / height;
		int x2 = (cutLeft + cutWidth) * originalW / width;
		int y2 = (cutTop + cutHeight) * originalH / height;
		int newW = x2 - x;
		int newH = y2 - y;
		logger.debug("sourceRegion(x=" + x + ",y=" + y + ",w="
				+ newW + ",y=" + newH);
		logger.debug("size=w=" + newW + ",h=" + newH);
		//sourceRegion//x轴、y轴，裁剪宽、裁剪
		Thumbnails.of(path).sourceRegion(x, y, newW, newH).size(newW, newH)
		.toFile(path);
		logger.debug("done of cutImage for " + path);
	}
	
	public static int [] getWidthHeight(String path) throws IOException {
		logger.debug("start to get image width and height");
		File file = new File(path);
		try {
			BufferedImage sourceImg =ImageIO.read(new FileInputStream(file));
			int w = sourceImg.getWidth();
			int h = sourceImg.getHeight();
			logger.debug("original w=" + w + ", h=" + h);
			int [] ret = new int [2];
			ret[0] = w;
			ret[1] = h;
			logger.debug("done");
			return ret;
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			throw new IOException(e.getMessage(), e);
		} 
	}
	
	public static int [] getWidthHeight(InputStream is) throws IOException {
		logger.debug("start to get image width and height");
//		File file = new File(path);
		try {
			BufferedImage sourceImg =ImageIO.read(is);
			int w = sourceImg.getWidth();
			int h = sourceImg.getHeight();
			logger.debug("original w=" + w + ", h=" + h);
			int [] ret = new int [2];
			ret[0] = w;
			ret[1] = h;
			logger.debug("done");
			return ret;
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			throw new IOException(e.getMessage(), e);
		} 
	}
	
	public static void logoIconMerge(String logoPath, String iconPath,
			int logoW, int logoH, int iconLeft, int iconTop, int iconW,
			int iconH) throws IOException
	{
		int[] data = getWidthHeight(logoPath);
		int originalLogoW = data[0];
		int originalLogoH = data[1];
		data = getWidthHeight(iconPath);
		int originalIconW = data[0];
		int originalIconH = data[1];
		//处理icon的大小，变成BufferedImage
//		ImageIO.read(iconIs).;
		BufferedImage iconBf = Thumbnails.of(iconPath).size(originalLogoW * iconW / logoW, 
				originalLogoH * iconH / logoH).asBufferedImage();
		Position pos = new IconPosition((float)iconLeft / (float)logoW, (float)iconTop / (float)logoH);
		Thumbnails.of(logoPath).size(originalLogoW, originalLogoH)
			.watermark(pos, iconBf, 1.0f).toFile(logoPath);
	}
	
	public static String saveInputStream(InputStream is, String folder, String suffix) 
			throws IOException
	{
		String file = UUID.randomUUID().toString();
		String realFile = folder + File.separator + file + suffix;
		FileOutputStream fos = new FileOutputStream(new File(realFile));
		byte[] content = new byte[1024];
		int num;
		while((num = is.read(content)) != -1) {
			fos.write(content, 0, num);
		}
		fos.close();
		return realFile;
	}

}
